package com.garen.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//资源加载
@Deprecated
public class ReadResource {
	
	private static Log log = LogFactory.getLog(ReadResource.class);   
	public ReadResource(String[] folderPaths,String exFile,String fileType,
			boolean subfolder,boolean cache){
		this(folderPaths,"",exFile,fileType,subfolder,cache);
	}
	//inFile优先考虑
	public ReadResource(String[] folderPaths,String inFile,String exFile,String fileType,
			boolean subfolder,boolean cache){
		this.inFiles = inFile.split(",");
		this.folderPaths = folderPaths;
		this.exFiles = exFile.split(",");
		this.fileType = fileType;
		this.subfolder = subfolder;
		//this.cache = cache;
	}
	private String[] folderPaths;//要遍历的文件夹
	private String[] exFiles;//派出的文件
	private String[] inFiles;//排在前面的文件
	private String fileType;//文件类型
	private boolean subfolder;//是否遍历子目录
	private String basePath = null;//根目录
	private ByteArrayOutputStream bout = null;
	private ByteArrayOutputStream boutFirst = null;
	private boolean moduleFlag = true;//true 正常封装，false附加模块名称
	
	public byte[] load(){
		return loadStream().toByteArray();
	}
	
	public ByteArrayOutputStream loadStream(){
		bout = new ByteArrayOutputStream();
		boutFirst = new ByteArrayOutputStream();
		for(String folder : folderPaths){
			File f = new File(folder);
			if(f.exists() == false) continue;
			if(f.isDirectory()) readFolder(f);
			else readFile(f);
		}
		try {
			bout.writeTo(boutFirst);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return boutFirst;
	}
	
	public ByteArrayOutputStream loadStreamEx(ByteArrayOutputStream jsout,String basePath,String[] fpaths){
		moduleFlag = false;
		this.basePath = basePath;
		bout = new ByteArrayOutputStream();
		boutFirst = jsout;
		for(String folder : fpaths){
			File f = new File(folder);
			if(f.exists() == false) continue;
			if(f.isDirectory()) readFolder(f);
			else readFile(f);
		}
		try {
			bout.writeTo(boutFirst);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return boutFirst;
	}
	//遍历文件夹
	private void  readFolder(File folder){
		File [] files = folder.listFiles();
		for(File f : files){
			if(f.isDirectory() && subfolder) readFolder(f);
			else readFile(f);
		}
	}
	//匹配名称
	private static boolean matchName(String []files ,String fileName){
		boolean ret = false;
		for(String name : files){
			if(fileName.equals(name)){
				ret = true;
				break;
			}
		}
		return ret;
	}
	//读取文件内容
	private void   readFile(File file){
		String fileName = file.getName();
		String extName = FileUtils.getExtName(fileName);
		if(false == fileType.equals(extName)) return;
		if(matchName(exFiles,fileName)) return;//不需要加载的文件
		ByteArrayOutputStream out = matchName(inFiles, fileName)?boutFirst:bout;
		try {
			byte [] fileBody = FileUtils.readbodydata(new FileInputStream(file), 0);
			out.write(("/***"+fileName+"***/\r\n").getBytes());
			if(false == moduleFlag){
				String path = file.getAbsolutePath().replace(basePath, "");
				path = path.replaceAll("\\\\", "/");
				String mname = "garen_define(\""+ path +"\",";
				out.write(mname.getBytes());
				out.write(fileBody);
				out.write(");\r\n".getBytes());
			}else{
				out.write(fileBody);
				out.write("\r\n".getBytes());
			}
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}

}
